﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Spelprojekt.View
{
    class ParticleSystem
    {
        Particle[] m_particles = new Particle[400];
        int m_index = 0;
        float m_gravity = 0.82f;

        //the spread of the explosion on X and Y
        int m_speedXLimit = 10;
        int m_speedYLimit = 10;

        public static Random m_random = new Random();

        float m_totalGameTime = 0;

        public ParticleSystem()
        {
            for (int i = 0; i < m_particles.Length; i++)
            {
                m_particles[i] = new Particle();
            }
        }

        //returns the active particles
        public List<Particle> GetParticles()
        {
            List<Particle> particles = new List<Particle>();

            for (int i = 0; i < m_particles.Length; i++)
            {
                if (m_particles[i].IsActive())
                {
                    particles.Add(m_particles[i]);
                }
            }

            return particles;
        }

        public void CreateBloodSplatter(Vector2 a_pos, Vector2 a_speed)
        {
            //calculate the direction of the splatter using randomness and the players speed
            Vector2 speed = new Vector2(-a_speed.X - m_random.Next(-m_speedXLimit, m_speedXLimit),
                                          -a_speed.Y - m_random.Next(-m_speedYLimit, m_speedYLimit));

            Vector2 acceleration = new Vector2(0, m_gravity);


            //create particle
            m_particles[m_index] = new Particle(a_pos, speed, acceleration, (float)m_random.NextDouble() / 1.5f, Color.White, 3.0f);

            //needed to not go out of index boundary and to create new particles
            m_index++;
            if (m_index >= m_particles.Length)
            {
                m_index = 0;
            }
        }

        public void RunCloudLeft(Vector2 a_pos, Vector2 a_speed)
        {
            //calculate the direction of the splatter using randomness and the players speed            
            Vector2 speed = new Vector2(-a_speed.X - m_random.Next(-m_speedXLimit, m_speedXLimit),
                                         -a_speed.Y - m_random.Next(-m_speedYLimit, m_speedYLimit));     

            Vector2 acceleration = new Vector2(m_gravity, 0);


            //create particle
            m_particles[m_index] = new Particle(a_pos, a_speed, acceleration, (float)m_random.NextDouble() / 2f, Color.White, 0.50f);

            //needed to not go out of index boundary and to create new particles
            m_index++;

            if (m_index >= m_particles.Length)
            {
                m_index = 0;
            }
        }

        public void RunCloudRight(Vector2 a_pos, Vector2 a_speed)
        {
            //calculate the direction of the splatter using randomness and the players speed            
            Vector2 speed = new Vector2(-a_speed.X - m_random.Next(-m_speedXLimit, m_speedXLimit),
                                         -a_speed.Y - m_random.Next(-m_speedYLimit, m_speedYLimit));

            Vector2 acceleration = new Vector2(m_gravity, 0);


            //create particle
            m_particles[m_index] = new Particle(a_pos, a_speed, acceleration, (float)m_random.NextDouble() / 2f, Color.White, 0.50f);

            //needed to not go out of index boundary and to create new particles
            m_index++;

            if (m_index >= m_particles.Length)
            {
                m_index = 0;
            }
        }


        public void Update(float a_elapsedTime)
        {
            m_totalGameTime += a_elapsedTime;

            for (int i = 0; i < m_particles.Length; i++)
            {
                if (m_particles[i].IsActive())
                {
                    m_particles[i].UpdateParticle(a_elapsedTime);
                }
            }
        }
    }
}
